package com.jbl.ma.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jbl.commonutils.R;
import com.jbl.ma.entity.UserApply;
import com.jbl.ma.entity.UserReject;
import com.jbl.ma.entity.UserRejectReason;
import com.jbl.ma.mapper.UserRejectMapper;
import com.jbl.ma.mapper.UserRejectReasonMapper;
import com.jbl.ma.service.UserApplyService;
import com.jbl.ma.service.UserRejectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbl.ma.vo.UserRejectVo;
import com.jbl.te.entity.Teacher;
import com.jbl.te.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@Service
public class UserRejectServiceImpl extends ServiceImpl<UserRejectMapper, UserReject> implements UserRejectService {
    @Autowired
    private UserApplyService userApplyService;
    @Autowired
    private UserRejectReasonMapper rejectReasonMapper;
    @Autowired
    private TeacherService teacherService;

    @Transactional
    @Override
    public boolean reject(UserRejectVo vo) {
        if (vo != null) {
            // 传来applyId 修改申请表 isVerified==2
            UserApply userApply = userApplyService.getById(vo.getApplyId());
            userApply.setIsVerified(2);

            userApplyService.update(userApply, new UpdateWrapper<UserApply>().eq("apply_id", vo.getApplyId()));
            // 添加驳回
            UserReject reject = new UserReject();
            UserRejectReason rejectReason = new UserRejectReason();

            if (vo.getRejectType().equals("-1")) {
                //添加到reason表
                rejectReason.setRejectReason(vo.getRejectReason());
                rejectReasonMapper.insert(rejectReason); // 生成type,存入reject表中
                reject.setApplyId(vo.getApplyId());
                reject.setRejectType(rejectReason.getRejectType());
                baseMapper.insert(reject);
            } else {
                // reason表中已有数据不需要添加到reason表，直接添加到reject
                reject.setApplyId(vo.getApplyId());
                reject.setRejectType(vo.getRejectType());
                baseMapper.insert(reject);// 用dao方法才会插入时间
            }
            return true;
        } else {
            return false;
        }
    }

    @Transactional
    @Override
    public boolean rejectBatch(List<String> applyIds) {
        // 传来 applyId 修改 isVerified==2
        // System.out.println("applyids: " + applyIds);
        if (applyIds != null) {
            List<UserReject> list = applyIds.stream().map(applyId -> {
                UserApply userApply = userApplyService.getById(applyId);
                userApply.setIsVerified(2);

                userApplyService.update(userApply,
                        new UpdateWrapper<UserApply>().eq("apply_id", applyId)
                );

                // 添加驳回
                UserReject reject = new UserReject();
                reject.setApplyId(applyId);
                reject.setRejectType("-1");
                baseMapper.insert(reject);// 用dao方法才会插入时间
                return reject;
            }).collect(Collectors.toList());

            if (list.size() > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
